//Arduino Code /* /* arduino_pwm Demonstrates the control of analog output (PWM) pins of an Arduino board running the StandardFirmata firmware. Moving the mouse horizontally across the sketch changes the output on pins 9 (value is proportional to the mouse X coordinate) and 11 (inversely porportional to mouse X). To use: * Using the Arduino software, upload the StandardFirmata example (located in Examples > Firmata > StandardFirmata) to your Arduino board. * Run this sketch and look at the list of serial ports printed in the message area below. Note the index of the port corresponding to your Arduino board (the numbering starts at 0). (Unless your Arduino board happens to be at index 0 in the list, the sketch probably won't work. Stop it and proceed with the instructions.) * Modify the "arduino = new Arduino(...)" line below, changing the number in Arduino.list()[0] to the number corresponding to the serial port of your Arduino board. Alternatively, you can replace Arduino.list()[0] with the name of the serial port, in double quotes, e.g. "COM5" on Windows or "/dev/tty.usbmodem621" on Mac. * Connect LEDs or other outputs to pins 9 and 11. * Run this sketch and move your mouse horizontally across the screen. For more information, see: http://playground.arduino.cc/Interfacing/Processing */ import processing.serial.*; import cc.arduino.*; Arduino arduino; int u_pin= 11; int A0; void setup() { size(1000, 1000); // Prints out the available serial ports. println(Arduino.list()); // Modify this line, by changing the "0" to the index of the serial // port corresponding to your Arduino board (as it appears in the list // printed by the line above). arduino = new Arduino(this, Arduino.list()[0], 57600); // Alternatively, use the name of the serial port corresponding to your // Arduino (in double-quotes), as in the following line. //arduino = new Arduino(this, "/dev/tty.usbmodem621", 57600); } void draw() { fill(constrain(255-mouseY/2, 0, 255), constrain(arduino.analogRead(A0),0,255), constrain(255 - mouseX/2,0,255)); ellipse(arduino.analogRead(A0),arduino.analogRead(A0), arduino.analogRead(A0), arduino.analogRead(A0)); println(constrain(255-mouseY/2, 0, 255), constrain(arduino.analogRead(A0),0,255), constrain(255 - mouseX/2,0,255)); // Output analog values (PWM waves) to digital pins 9 and 11. // Note that only certain Arduino pins support analog output (PWM). // See the documentation for your board for details. arduino.analogWrite(11, constrain(255-mouseY/2, 0, 255)); arduino.analogWrite(10, constrain(255 - mouseX / 2, 0, 255)); arduino.analogWrite(9, constrain(arduino.analogRead(A0),0,255)); } void pulseIn (int a, int b){ }
//Processing /* arduino_pwm Demonstrates the control of analog output (PWM) pins of an Arduino board running the StandardFirmata firmware. Moving the mouse horizontally across the sketch changes the output on pins 9 (value is proportional to the mouse X coordinate) and 11 (inversely porportional to mouse X). To use: * Using the Arduino software, upload the StandardFirmata example (located in Examples > Firmata > StandardFirmata) to your Arduino board. * Run this sketch and look at the list of serial ports printed in the message area below. Note the index of the port corresponding to your Arduino board (the numbering starts at 0). (Unless your Arduino board happens to be at index 0 in the list, the sketch probably won't work. Stop it and proceed with the instructions.) * Modify the "arduino = new Arduino(...)" line below, changing the number in Arduino.list()[0] to the number corresponding to the serial port of your Arduino board. Alternatively, you can replace Arduino.list()[0] with the name of the serial port, in double quotes, e.g. "COM5" on Windows or "/dev/tty.usbmodem621" on Mac. * Connect LEDs or other outputs to pins 9 and 11. * Run this sketch and move your mouse horizontally across the screen. For more information, see: http://playground.arduino.cc/Interfacing/Processing */ import processing.serial.*; import cc.arduino.*; Arduino arduino; int u_pin= 11; int A0; float pulseIn(int,int); void setup() { size(1000, 1000); // Prints out the available serial ports. println(Arduino.list()); // Modify this line, by changing the "0" to the index of the serial // port corresponding to your Arduino board (as it appears in the list // printed by the line above). arduino = new Arduino(this, Arduino.list()[0], 57600); // Alternatively, use the name of the serial port corresponding to your // Arduino (in double-quotes), as in the following line. //arduino = new Arduino(this, "/dev/tty.usbmodem621", 57600); } void draw() { float duration, distance; arduino.digitalWrite(12, 1); // Added this line delay(2); // Added this line arduino.digitalWrite(12, 0); // delayMicroseconds(1000); - Removed this line delay(10); // Added this line arduino.digitalWrite(12, 0); duration = pulseIn(13, 0); distance = (duration/2) / 29.1; if (distance < 4) { // This is where the LED On/Off happens arduino.digitalWrite(10,255); } fill(constrain(255-mouseY/2, 0, 255), constrain(arduino.analogRead(A0),0,255), constrain(255 - mouseX/2,0,255)); ellipse(arduino.analogRead(A0),arduino.analogRead(A0), arduino.analogRead(A0), arduino.analogRead(A0)); println(constrain(255-mouseY/2, 0, 255), constrain(arduino.analogRead(A0),0,255), constrain(255 - mouseX/2,0,255)); // Output analog values (PWM waves) to digital pins 9 and 11. // Note that only certain Arduino pins support analog output (PWM). // See the documentation for your board for details. arduino.analogWrite(11, constrain(255-mouseY/2, 0, 255)); arduino.analogWrite(10, constrain(255 - mouseX / 2, 0, 255)); arduino.analogWrite(9, constrain(arduino.analogRead(A0),0,255)); }
//Arduino code #include#define TRIGGER_PIN 12 #define ECHO_PIN 13 #define LED_PIN 9 #define LED_PIN2 10 #define LED_PIN3 11 #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); byte brightness; long rangeInCentimeters; void setup() { mySerial.begin(115200); // Open serial monitor at 115200 baud to see ping results. pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN2, OUTPUT); pinMode(LED_PIN3, OUTPUT); } void loop() { delay(20); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds. int cm = sonar.ping_cm(); int rangeInCentimeters = (uS / US_ROUNDTRIP_CM); if (rangeInCentimeters > 50 && rangeInCentimeters < 100) { analogWrite(LED_PIN, 0); analogWrite(LED_PIN2, 255); analogWrite(LED_PIN3, 255); mySerial.println("a"); } else if (rangeInCentimeters >= 10 && rangeInCentimeters <=50){ analogWrite(LED_PIN, 0); analogWrite(LED_PIN2, 0); analogWrite(LED_PIN3, 255); mySerial.println("b"); } else if (rangeInCentimeters >100){ // set the brightness of the LED: analogWrite(LED_PIN, 255); //blue analogWrite(LED_PIN2, 0); //green analogWrite(LED_PIN3, 0); //red } else{} }